package m3_publishsubscribe;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接工厂
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("192.168.126.130");
        cf.setUsername("admin");
        cf.setPassword("admin");
        //建立连接
        Connection connection = cf.newConnection();
        //建立通道
        Channel ch = connection.createChannel();
        //1定义交换机
        ch.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        /*String queue= UUID.randomUUID().toString();
        //非持久 独占(排他) 自动删除
        ch.queueDeclare(queue,false,true,true,null);*/
        //2定义随机队列
        String queue = ch.queueDeclare().getQueue();
        //3绑定交换机
        ch.queueBind(queue, "logs", "");
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody(), "UTF-8");
                System.out.println("收到：" + msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        //消费数据
        ch.basicConsume(queue, true, deliverCallback, cancelCallback);
    }


}
